package com.rqf.mr.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.mr.service.IAssistService;

@Service("AssistService")
public class AssistService implements IAssistService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	private SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public List<Map<String, Object>> getAssistTreatListFromEhisView(String orgId,
			String inpatiId) {
		if (StringUtils.isEmpty(orgId) || StringUtils.isEmpty(inpatiId)) {
			return null;
		}
		String sql = "select * from view_ehis_fzzl dd where dd.LOCORGID='" +orgId+ "'  and dd.INPATIID='" + inpatiId + "'";
		List<Map<String, Object>> notExecuteList = jdbcTemplate.queryForList(sql);
		List<Map<String, Object>> executedList = getAssistTreatListFromTabAssistTreat(inpatiId);
		for (int i = 0; i < notExecuteList.size(); i++) {
			String freqId = notExecuteList.get(i).get("FREQID").toString();
			if ( freqId.equals("st") ) {
				for (int j = 0; j < executedList.size(); j++) {
					if ( notExecuteList.get(i).get("ORDID").equals(executedList.get(j).get("ORDID")) ) {
						notExecuteList.remove(i);
						executedList.remove(j);
						i--;
						break;
					}
				}
			}
		}
		return notExecuteList;
	}
	
	@Override
	public List<Map<String, Object>> getAssistTreatListFromTabAssistTreat(
			String inpatiId) {
		if ( StringUtils.isEmpty(inpatiId) ) {
			return null;
		}
		String sql = "select * from ASSIST_TREAT dd where dd.INPATIID='" + inpatiId + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
	
		for (int i = 0; i < list.size(); i++) {//时间格式化
			list.get(i).put("SUBMITTIME", time.format((Date)list.get(i).get("SUBMITTIME")));
		}
		return list;
	} 

	@Override
	public Boolean executeOrderByNurseLoginId(String orderId,
			String nurseLoginId) {
		if ( StringUtils.isEmpty(orderId) || StringUtils.isEmpty(nurseLoginId) ) {
			return false;
		}
		int result =0;
		DataSource dataSource = jdbcTemplate.getDataSource();  
		Connection conn=null;  
		try {
			conn=dataSource.getConnection();
			CallableStatement cs = conn.prepareCall("{call ASSIST_TREAT_ADD(?,?,?)}");   
			cs.setString(1, orderId);  
			cs.setString(2, nurseLoginId); 
			cs.registerOutParameter(3, OracleTypes.NUMBER);
			cs.execute();  
			result = cs.getInt(3);//获取单个返回值
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}  
		if ( result>0 ) {
			return true;
		}else {
			return false;
		}
	}
	
	@Override
	public List<Map<String, Object>> getDrugInfoByOrderid(String orderId) {
		if ( StringUtils.isEmpty(orderId) ) {
			return null;
		}
		String sql = "select * from view_ehis_fzzl "
				+ "where ORDID='" + orderId + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

}
